package com.wilschrader.demarche.stages;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Set;

import com.wilschrader.demarche.Fleet;
import com.wilschrader.demarche.Simulation;
import com.wilschrader.demarche.Stage;
import com.wilschrader.demarche.Unit;
import com.wilschrader.demarche.Fleet.Type;
import com.wilschrader.demarche.util.MersenneTwisterFast;

/**
 * Combat, IN SPACE!
 * 
 * @author Wil Schrader
 * @modifications
 * 	08172011	Init revision.
 * 
 */
public class SpaceCombat implements Stage {

	@Override
	public void execute(MersenneTwisterFast mt, LinkedHashMap<Type, Fleet> fleets) {
		Set<Type> fleetSet = fleets.keySet();
		int round = 1;
		
		while (true) {
			
			for (Type defendingType : fleetSet) {
				Fleet defendingFleet = fleets.get(defendingType);
				
				for (Type attackingType : fleetSet) {
					if (attackingType != defendingType) {
						Fleet attackingFleet = fleets.get(attackingType);
						
						Simulation.debugOutput("Fleet (" + attackingFleet.getName() + ") performing space combat round " + round + " on fleet (" + defendingFleet.getName() +").");
						
						ArrayList<Unit> units = attackingFleet.getActiveUnits(Unit.SPACE_UNITS);
						
						for (Unit unit: units) {
							defendingFleet.registerHits(unit.doAttack(mt));
						}
					}
				}
			}
			
			for (Type type : fleetSet) {
				if (!fleets.get(type).allocateHits(Unit.SPACE_UNITS))
					return;
			}
			
			round++;
		}
	}

}
